<?php

/** Gpc_Controller_BaseStatsModel */
require_once ( 'Gpc/Controller/BaseStatsModel.php' );

/* All the different model classses this class uses */
require_once ( 'Eras.php' );
require_once ( 'Zones.php' );

class Admin_ZoneController extends Gpc_Controller_BaseStatsModel {

   /**
    * Initiaizes this controler specific variables.
    */
   public function init() {

      $this->modelName = 'Zones';
      $this->returnController = 'zone';
   
   }

   public function indexAction() {

      // Generate signature for delete requests.
      $randomKey = md5( unique_id() . time() );
      
      $session = Zend_Registry::get( 'session' );
      $this->view->signature = $session->signature = $randomKey;
      
      /* Defines YUI Libraries to load */
      $this->view->yui_libraries = array ( 'utilities', 'selector', 'json', 'logger', 'container' );
      // Objectize this?
      $this->view->loader_extras = array ( 
                                            'accordioncss' => array (
                                                      'name' => 'accordioncss',
                                                      'fullPath' => "../bubbling/accordion/assets/accordion.css",
                                                      'type' => 'css'),
                                            'bubbling' => array (
                                                      'name' => 'bubbling',
                                                      'requires' => array('utilities'),
                                                      'type' => 'js',
                                                      'fullPath' => "../bubbling/bubbling/bubbling.js" ),
                                            'accordion' => array ( 
                                                      'name' => 'accordion',
                                                      'requires' => array('utilities', 'accordioncss', 'bubbling'),
                                                      'type' => 'js',
                                                      'fullPath' => "../bubbling/accordion/accordion.js" ),
                                           ); 
      
      $this->view->eras = Eras::getAll( null, 'name' );
      $this->view->eraSelectList = array ( );
      
      foreach ( $this->view->eras as $era ) {
         $this->view->eraSelectList[$era['id']] = $era['name'];
      }
   }

   /**
    * Filters and validates data for the `stats` table.
    * 
    * @return array Returns an array of stats data after filtering.
    */
   protected function _filter() {

      /* Validation Process */
      $filters = array ( 'zone_era' => 'Digits', 'zone_name' => 'StringTrim' );
      $validators = array ( 'zone_era' => array ( array ( 'digits', true ), 'presence' => 'required' ), 'zone_name' => array ( array ( 'alnum', true ), 'presence' => 'required' ) );
      
      /*
       * Validate input
       *  -- I would prefer a more built in way to validate input, vs uses the actual
       *     super global.
       */
      $input = new Zend_Filter_Input( $filters, $validators, $this->getRequest()->getParams() );
      
      if ( ! $input->isValid() ) { // Insure it's valid input.
         throw new Exception( 'Invalid input for _filter(): ' . print_r( $input->getMessages(), true ), - 432 );
      }
      
      /* Build data array for passing to model class */
      $data = array ( 'eras_id' => $input->zone_era, 'name' => $input->zone_name );
      
      return $data;
   }
}
